package com.example.juc;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @Description
 * @Date 2021/7/24
 * @Created by Jonathan
 */
@Slf4j
public class ABAProblem {
    static AtomicReference<String> reference = new AtomicReference<>("A");

    public static void main(String[] args) throws InterruptedException {
        log.info("start main");
        String s = reference.get();
        other();
        TimeUnit.SECONDS.sleep(1);
        log.info("A->C,ret:{}", reference.compareAndSet(s, "C"));
    }

    public static void other() {
        new Thread(() -> {
            String pre = reference.get();
            log.info("A->B,ret:{}", reference.compareAndSet(pre, "B"));
            pre = reference.get();
            log.info("B->A,ret:{}", reference.compareAndSet(pre, "A"));
        }).start();
    }
}
